<?php

/**
 * 文章分类管理
 */
require_once (HOME . '/include/base/BackendController.php');
class acategory extends BackendController {
	private $mod_acategory;
	function __construct() {
		parent::__construct ();
		$this->acategory ();
	}
	function acategory() {
		$this->mod_acategory = spClass ( 'm_acategory' );
	}
	
	/**
	 * 显示文章分类
	 */
	function index() {
		$this->tpl_title = "文章分类管理";
		
		$result = spClass ( "m_acategory" )->spLinker ()->findAll ();
		// array(
		// 'child' => array('acategory_id' => '4')
		// 'acategory_id' =>'1'
		// ),
		// '',
		// '',
		// 'acategory_name,acategory_id'
		
		// dump ( $result );
		
		$this->assign ( 'acategoryList', $result );
		$this->assign ( 'action', 'list' );
		$this->display ( 'acategory.layout.html' );
	}
	/**
	 * 添加文章分类
	 */
	function add() {
		if(!IS_POST){
			$this->assign('action', 'add');
			$this->display('acategory.layout.html');
		} else {
			$acategory_model = $this->mod_acategory;
			$newrow = array ( // 这里制作新增记录的值
					'parent' => $this->spArgs ( 'parent' ),
					'name' => $this->spArgs ( 'name' ),
					'slug' => $this->spArgs ( 'slug' ),
					'sort' => $this->spArgs ( 'sort' ),
					'status' => $this->spArgs ( 'status' ),
					'thumb' => $this->spArgs ( 'thumb' ),
					'description' => $this->spArgs ( 'description' ) 
			);
			$acategory_model->create ( $newrow );
			$this->success ( "增加分类成功！", spUrl ( "category", "index" ) );
			// TODO 提交的参数正确
		}
	}
	/**
	 * 编辑文章分类
	 */
	function edit() {
		$this->tpl_title = "编辑分类";
		
		// 这里先判断是否传入了id
		if (! IS_POST) {
			// 无id则直接跳转回首页
			$this->jump ( spUrl ( "category", "index" ) );
		} else {
			if (! IS_AJAX) {
				if (false === $this->spArgs ( "id" )) {
					$this->error ( "错误的id", spUrl ( "acategory", "index" ) );
				} else {
					// 查找表，获取信息
					$info = spClass ( "lib_category" )->find ( array (
							'id' => $this->spArgs ( "id" ) 
					) );
					$this->assign ( "info", $info );
				}
			} else { // ajax提交
				if ($post_ajax = json_decode ( $this->spArgs ( 'ajax' ) )) {
					
					//TODO　下面的array() 相应字段
					// 分类id 分类 内容
					$result = array_merge ( array (), $post_ajax );
				} else {
// 					TODO json_error 请完善父类函数
// 					$this->json_error('提交的数据不正确');
				}
			}
		}
		
		$this->display ( "admin/category_write.html" );
	}
	/**
	 * 删除文章分类
	 */
	function drop() {
		// 这里先判断是否传入了id
		if ($id = $this->spArgs ( "id" )) {
			// 执行删除
			spClass ( "m_acategory" )->delete ( array (
					'id' => $id 
			) );
			$this->success ( "删除成功！", spUrl ( "category", "index" ) );
		} else {
			// 无id则直接跳转回首页
			$this->jump ( spUrl ( "category", "index" ) );
		}
	}
	
}
?>